package simulation;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;


/**
 * Contains a Canvas, and menu that respond to change in games.
 * 
 * @author Rex
 **/
public class GameWindow extends JFrame {

    private static final int NUMBER_OF_GAME = 2;

    private int myInfoHeight = 100;
    private int myWidth;
    private int myHeight;

    private Canvas myCanvas;
    private ArrayList<ResourceBundle> myResources;
    private JMenuBar myMenuBar;

    /**
     * Constructor for the game window.
     * 
     * @param width of the window
     * @param height of the window
     * @param title of the window
     */
    public GameWindow (int width, int height, String title) {
        myWidth = width;
        myHeight = height;
        setTitle(title);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(width, height);
        setVisible(true);
        setResizable(true);
        myResources = new ArrayList<ResourceBundle>();
    }

    /**
     * Imports an image to use.
     * 
     * @param resources the package from which the image comes
     * @param address the name of the image
     * @return
     */
    public static Image importImage (ResourceBundle resources, String address) {
        ImageIcon myImageIcon = new ImageIcon(resources.getString(address));
        return myImageIcon.getImage();
    }

    /**
     * Adds a resource bundle to this game window.
     * 
     * @param resource the name of the bundle to add
     */
    public void addResourceBundle (String resource) {
        myResources.add(ResourceBundle.getBundle("resources." + resource));
    }

    @SuppressWarnings("serial")
    public void addChoiceMenu () {
        myMenuBar = new JMenuBar();
        JMenu myMenu = new JMenu(myResources.get(0).getString("GameChoice"));
        myMenu.add(new AbstractAction(myResources.get(0).getString("FirstGame")) {
            @Override
            public void actionPerformed (ActionEvent e) {
                startGame(0);
            }
        });
        myMenu.add(new AbstractAction(myResources.get(0).getString("SecondGame")) {
            @Override
            public void actionPerformed (ActionEvent e) {
                startGame(1);
            }
        });
        myMenuBar.add(myMenu);
        setJMenuBar(myMenuBar);
    }

    /**
     * Adds a menu to the window.
     * 
     * @param name of the menu
     */
    public void addMenu (String name) {
        myMenuBar.add(new JMenu(myResources.get(0).getString(name)));
    }

    /**
     * Adds a choice to start the game.
     */
    public void renderGame () {
        addChoiceMenu();
    }

    /**
     * Refreshes the game window.
     */
    public void refreshScreen () {
        Dimension mySize = getSize();
        setSize(new Dimension(mySize.width, mySize.height - 1));
        setSize(mySize);
    }

    /**
     * Starts the selected game.
     */
    public void startGame (int resource) {
        if(myCanvas != null )remove(myCanvas);
        myCanvas = new Canvas(myResources.get(resource), myWidth, myHeight - myInfoHeight, this);
        add(myCanvas, BorderLayout.NORTH);
    }
}
